﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace DbSharp.SupportLib.Crypto
{
    public class SymmetricCryptoService
    {
  

        /// <summary>
        /// Get key 
        /// </summary>
        /// <returns>return 256 bits key</returns>
        private byte[] GetLegalKey()
        {
            string tempKey = "Guz(%&hj7x89H$yuBI0456Ftma_T5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7f";
            using (SHA256Managed hash256 = new SHA256Managed())
            {
                return hash256.ComputeHash(Encoding.UTF8.GetBytes(tempKey));
            }
        }


        /// <summary>
        /// Get initial vector
        /// </summary>
        /// <returns>return 128 bits IV</returns>
        private byte[] GetLegalIV()
        {
            string tempIV = "E4ghj*Ghg7!rNIfb&_95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
            using (MD5 hash128 = MD5.Create())
            {
                return hash128.ComputeHash(Encoding.UTF8.GetBytes(tempIV));
            }
        }


        /// <summary>
        /// Encrypt method
        /// </summary>
        /// <param name="Source"></param>
        /// <returns></returns>
        public string Encrypto(string base64Source)
        {
            byte[] bytIn = Convert.FromBase64String(base64Source);
            using (SymmetricAlgorithm cryptoService = new AesManaged())
            using (MemoryStream ms = new MemoryStream())
            {
                cryptoService.Key = GetLegalKey();
                cryptoService.IV = GetLegalIV();
                ICryptoTransform encrypto = cryptoService.CreateEncryptor();
                using (CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write))
                {
                    cs.Write(bytIn, 0, bytIn.Length);
                    cs.FlushFinalBlock();
                }
                byte[] bytOut = ms.ToArray();
                return Convert.ToBase64String(bytOut);
            }
        }


        /// <summary>
        /// decrypt method
        /// </summary>
        /// <param name="Source"></param>
        /// <returns></returns>
        public string Decrypto(string base64Source)
        {
            byte[] bytIn = Convert.FromBase64String(base64Source);
            using (SymmetricAlgorithm cryptoService = new AesManaged())
            using (MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length))
            {
                cryptoService.Key = GetLegalKey();
                cryptoService.IV = GetLegalIV();
                ICryptoTransform encrypto = cryptoService.CreateDecryptor();
                using (CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read))
                using (StreamReader sr = new StreamReader(cs, Encoding.UTF8))
                {
                    return sr.ReadToEnd();
                }
            }
        }
    }
}
 
